<?php
namespace Common\Model;
use Think\Model;

class ItemsModel extends Model
{
    protected $_auto = array(
        array('add_time', 'time', 1, 'function'),
		array('pass',1),
    );

    /**
     * 发布一个商品
     * $item 商品信息
     */
    public function publish($item) {
		$result['title']			= $item['title'];
		$result['pic_url']			= $item['pic_url'];
		$result['nick']				= $item['nick'];
		$result['coupon_price']		= $item['coupon_price'];
		$result['commission_rate']	= $item['commission_rate'];
		$result['price']			= $item['price'];
		$result['volume']			= $item['volume'];
		$result['commission']		= $item['commission'];
		$result['commission_num']	= $item['commission_num'];
		$result['commission_volume']= $item['commission_volume'];
		$result['click_url']		= $item['click_url'];
		$result['shop_click_url']	= $item['shop_click_url'];
        //已经存在？
		if(!$item['coupon_rate']){
			$item['coupon_rate']	= round(($item['coupon_price']/$item['price'])*10000, 0);
		}
        if ($this->where(array('num_iid'=>$item['num_iid']))->count()) {
		   $result['msg']	= '已经添加!';
           return $result;
        }

		$item['pass'] = 1;
		$item['coupon_start_time']	= strtotime($item['coupon_start_time']);
		$item['coupon_end_time']	= strtotime($item['coupon_end_time']);
        $this->create($item);
        $item_id = $this->add();
        if ($item_id) {
            $result['msg'] = '采集成功!';
            return $result;
        } else {
            $result['msg'] = '添加失败!';
            return $result;
        }
    }

	/**
     * ajax发布商品-爱淘客开放平台
     */
    public function ajax_itk_publish($item) {
        //是否存在
        if ($this->where(array('num_iid'=>$item['num_iid']))->count()) {
			if($item['recid']==1){
				unset($item['recid']);
			}else{
				unset($item['recid']);
				unset($item['cate_id']);
			}
			unset($item['astime']);
			$item['status'] ='underway';
			$item_id = $this->where(array('num_iid'=>$item['num_iid']))->save($item);
			if ($item_id) {
				return 0;
			} else {
				return 0;
			}
        }
		unset($item['recid']);
		$item['pass'] = 1;
		$item['status'] ='underway';
        $this->create($item);
        $item_id = $this->add();
        if ($item_id) {
		//	vendor('pscws4.pscws4', dirname(__FILE__), '.class.php');
        	import('Vendor.pscws4.pscws4');
			$pscws = new \PSCWS4();
			$pscws->set_dict(ITK_DATA_PATH . 'scws/dict.utf8.xdb');
			$pscws->set_rule(ITK_DATA_PATH . 'scws/rules.utf8.ini');
			$pscws->set_ignore(true);
			$pscws->send_text($item['title']);
			$wordss = $pscws->get_tops('10');
			$pscws->close();
			$ws = array();
			foreach ($wordss as $val) {
				$ws['name'] = $val['word'];
				$is  = M('Tags')->where($ws)->find();
				if($is){
					$score_data = array('tcount'=>array('exp','tcount+1'));
					M('Tags')->where($ws)->setInc('tcount',1);
				}else{
					M('Tags')->create($ws);
					M('Tags')->add();
				}
			}
            return 1;
        } else {
            return 0;
        }
    }


	/**
     * ajax发布商品
     */
    public function ajax_publish($item) {
		$result['title']			= $item['title'];
		$result['pic_url']			= $item['pic_url'];
		$result['nick']				= $item['nick'];
		$result['coupon_price']		= $item['coupon_price'];
		$result['commission_rate']	= $item['commission_rate'];
		$result['price']			= $item['price'];
		$result['volume']			= $item['volume'];
		$result['commission']		= $item['commission'];
		$result['commission_num']	= $item['commission_num'];
		$result['commission_volume']= $item['commission_volume'];
		$result['click_url']		= $item['click_url'];
		$result['shop_click_url']	= $item['shop_click_url'];
        //已经存在？
        if ($this->where(array('num_iid'=>$item['num_iid']))->count()) {
           return 0;
        }
		if(!$item['coupon_rate']){
			$item['coupon_rate']	= round(($item['coupon_price']/$item['price'])*10000, 0);
		}

		$item['pass'] = 1;
		$item['coupon_start_time']	= strtotime($item['coupon_start_time']);
		$item['coupon_end_time']	= strtotime($item['coupon_end_time']);
        $this->create($item);
        $item_id = $this->add();
        if ($item_id) {
            return 1;
        } else {
            return 0;
        }
    }

	/**
     * ajax发布商品
     */
    public function ajax_tb_publish($item) {
		//echo(print_r($item));
		$result['title']			= $item['title'];
		$result['pic_url']			= $item['pic_url'];
		$result['nick']				= $item['nick'];
		$result['coupon_price']		= $item['coupon_price'];
		$result['price']			= $item['price'];
		$result['ems']				= $item['ems'];
		$result['volume']			= $item['volume'];
		$result['coupon_rate']		= $item['coupon_rate'];
		$result['coupon_end_time']	= $item['coupon_end_time'];
		$result['coupon_start_time']= $item['coupon_start_time'];
		$robot_setting = F('robot_setting');
		if($robot_setting['ems'] >$item['ems']){
			return 0;
		}
        //已经存在？
        if ($this->where(array('num_iid'=>$item['num_iid']))->count()) {
           return 0;
        }
		if(!$item['coupon_rate']){
			$item['coupon_rate']	= round(($item['coupon_price']/$item['price'])*10000, 0);
		}
		$item['pass'] = 1;
        $this->create($item);
        $item_id = $this->add();
        if ($item_id) {
            return 1;
        } else {
            return 0;
        }
    }

	/**
     * ajax发布商品
     */
    public function ajax_yg_publish($item) {
		$result['title']			= $item['title'];
		$result['pic_url']			= $item['pic_url'];
		$result['nick']				= $item['nick'];
		$result['coupon_price']		= $item['coupon_price'];
		$result['price']			= $item['price'];
		$result['ems']				= $item['ems'];
		$result['volume']			= $item['volume'];
		$result['coupon_rate']		= $item['coupon_rate'];
		$result['coupon_end_time']	= $item['coupon_end_time'];
		$result['coupon_start_time']= $item['coupon_start_time'];
        //已经存在？
        if ($this->where(array('num_iid'=>$item['num_iid']))->count()) {
           return 0;
        }
		if(!$item['coupon_rate']){
			$item['coupon_rate']	= round(($item['coupon_price']/$item['price'])*10000, 0);
		}

		$item['pass'] = 1;
        $this->create($item);
        $item_id = $this->add();
        if ($item_id) {
            return 1;
        } else {
            return 0;
        }
    }


	public function get_tags_by_title($title, $num=10){
       // vendor('pscws4.pscws4', dirname(__FILE__), '.class.php');
		import('Vendor.pscws4.pscws4');
        $pscws = new \PSCWS4();
        $pscws->set_dict(ITK_DATA_PATH . 'scws/dict.utf8.xdb');
        $pscws->set_rule(ITK_DATA_PATH . 'scws/rules.utf8.ini');
        $pscws->set_ignore(true);
        $pscws->send_text($title);
        $words = $pscws->get_tops($num);
        $pscws->close();
        $tags = array();
        foreach ($words as $val) {
            $tags[] = $val['word'];
        }
        return $tags;
    }

    /**
     *返回出售状态
     */
    public function status($status, $stime ,$etime) {
		if(!$stime || !$etime){
			return 'buy';
		}
		if('1'== $status){
			return 'buy';
		}elseif('underway'!=$status){
			return 'sellout';
		}elseif($stime>time()){
			return 'start';
		}elseif($etime<time()){
			return 'gone';
		}elseif($stime<time()){
			return 'buy';
		}else{
			return 'buy';
		}

	}

	public function click_url($url,$num_iid) {
        if ($url && $num_iid) {
			 $this->where(array('num_iid'=>$num_iid))->save(array('click_url'=>$url));
            return true;
        }
    }

    //轻淘客
    public function goods_insert($items,$code)
    {
        $totalcoll = 0;
        $thiscount = 0;
        $items = json_decode($items);
        if($code == 200) {
            foreach($items->data as $item) {
                $res = $this->infos($item,$code);
                if($res>0){
                    $totalcoll++;
                }
                $thiscount++;
            }
        } else {
            foreach($items->data->list as $item) {
                $res = $this->infos($item,$code);
                if($res>0){
                    $totalcoll++;
                }
                $thiscount++;
            }
        }


        return $msg = [
            'msg' => '添加成功',
            'code' => 200,
            'totalcoll' => $totalcoll,
            'thiscount' => $thiscount,
        ];
    }


    public function infos($item,$code)
    {
        $arr = ['2' => 8,'3' => 7,'4' => 11,'5' => 3,'6' => 9,'7'=>6,'8' => 12,'9' => 13,'10' => 1,'11' => 4,'12' => 2];
        if($code == 200) {
            $data['cate_id'] = 25; //销量爆款
        } else {
            $data['cate_id'] = $arr[$item->goods_cat];
        }
        $data['num_iid'] = $item->goods_id;
        $data['pass'] = 1;
        if(substr($item->goods_pic,0,4) != 'http') {
            $data['pic_url'] = 'https:'.$item->goods_pic;
        } else {
            $data['pic_url'] = $item->goods_pic;
        }
        $data['title'] = $item->goods_title;
        $data['intro'] = $item->goods_introduce;
        $data['coupon_price'] = $item->goods_price;
        $data['volume'] = $item->goods_sales;
        $data['nick'] = $item->goods_short_title;
        $data['shop_type'] = $item->is_tmall== 1 ? 'B' : '';
        $data['commission'] = $item->goods_price * $item->commission / 100;
        $data['commission_rate'] = $item->commission_type;
        $data['sellerId'] = $item->seller_id;
        $data['price'] = $item->goods_price;
        $data['quan'] = $item->coupon_price;
        $data['quan_surplus'] = $item->coupon_number;
        $data['quan_receive'] = $item->coupon_over;
        $data['quan_condition'] = $item->coupon_condition;
        $data['coupon_start_time'] = strtotime($item->coupon_start_time);
        $data['coupon_end_time'] = strtotime($item->coupon_end_time);
        $data['quanurl'] = 'http://shop.m.taobao.com/shop/coupon.htm?seller_id='.$item->seller_id.'&&activity_id='.$item->coupon_id;


        $info = $this->where(['num_iid'=>$data['num_iid']])->find();

        if(is_null($info) && !is_null($data['pic_url'])) {
            $item_id = $this->add($data);
            if ($item_id) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    public function dataoke($items)
    {
        $totalcoll = 0;
        $thiscount = 0;

        $items = json_decode($items);
        foreach($items->result as $item) {
            $res = $this->addtaoke($item);
            if($res>0){
                $totalcoll++;
            }
            $thiscount++;
        }
        return $msg = [
            'msg' => '添加成功',
            'code' => 200,
            'totalcoll' => $totalcoll,
            'thiscount' => $thiscount,
        ];
    }

    public function addtaoke($item)
    {
        $arr = ['1'=> 1,'9'=>2,'10'=>4,'2'=>8,'3'=>7,'4'=>11,'5'=>5,'6'=>9,'7'=>6,'8'=>12];

        if(substr($item->Pic,0,4) != 'http') {
            $data['pic_url'] = 'https:'.$item->Pic;
        } else {
            $data['pic_url'] = $item->Pic;
        }
        $data['num_iid'] = $item->GoodsID;
        $data['pass'] = 1;
        $data['title'] = $item->Title;
        $data['nick'] = $item->D_title;
        $data['cate_id'] = $arr[$item->Cid];
        $data['coupon_price'] = $item->Price;
        $data['price'] = $item->Org_Price;
        $data['shop_type'] = $item->is_tmall== 1 ? 'B' : '';
        $data['volume'] = $item->Sales_num;
        $data['sellerId'] = $item->SellerID;
        $data['commission'] = $item->Org_Price * $item->Commission / 100;
        $data['commission_rate'] = $item->Commission_queqiao;
        $data['intro'] = $item->Introduce;
        $data['quan'] = $item->Quan_price;
        $data['coupon_end_time'] = strtotime($item->Quan_time);
        $data['quan_surplus'] = $item->Quan_surplus;
        $data['quan_receive'] = $item->Quan_receive;
        $data['quan_condition'] = $item->Quan_condition;
        $data['quanurl'] = $item->Quan_link;

        $info = $this->where(['num_iid'=>$data['num_iid']])->find();
        if(is_null($info) && !is_null($data['pic_url'])) {
            $item_id = $this->add($data);
            if ($item_id) {
                return 1;
            } else {
                return 0;
            }
        }
    }


    public function taozhushou($item)
    {
        $arr = ['1'=>1,'2'=>2,'3'=>4,'4'=>10,'5'=>9,'6'=>7,'7'=>8,'8'=>3,'9'=>12,'10'=>6,'11'=>13];

        if(substr($item->goods_pic,0,4) != 'http') {
            $data['pic_url'] = 'https:'.$item->goods_pic;
        } else {
            $data['pic_url'] = $item->goods_pic;
        }

        $data['num_iid'] = $item->goods_id;
        $data['pass'] = 1;
        $data['title'] = $item->goods_title;
        $data['nick'] = $item->goods_short_title;
        $data['intro'] = $item->goods_intro;
        $data['cate_id'] = $arr[$item->goods_cate_id];
        $data['price'] = $item->goods_price;
        $data['volume'] = $item->goods_sale_num;
        $data['coupon_price'] = $item->goods_price;
        $data['commission_rate'] = $item->commission_rate;
        $data['sellerId'] = $item->seller_id;
        $data['coupon_start_time'] = strtotime($item->coupon_start_time);
        $data['coupon_end_time'] = strtotime($item->coupon_end_time);
        $data['shop_type'] = $item->is_tmall== 1 ? 'B' : '';
        $data['quan'] = $item->coupon_amount;
        $data['quan_condition'] = $item->coupon_apply_amount;
        $data['quanurl'] = 'http://shop.m.taobao.com/shop/coupon.htm?seller_id='.$item->seller_id.'&&activity_id='.$item->coupon_id;

        $data['commission'] = $item->coupon_apply_amount * $item->commission_rate / 100;

        $info = $this->where(['num_iid'=>$data['num_iid']])->find();
        if(is_null($info) && !is_null($data['pic_url'])) {
            $item_id = $this->add($data);
            if ($item_id) {
                return 1;
            } else {
                return 0;
            }
        }
        
    }
}
